home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / winpcx2.arj / WINPCX.C < prev    next >
C/C++ Source or Header  |  1989-10-20  |  18KB  |  545 lines

  1. #include "WinPcx.inc"
  2.  
  3. /*~***************************************************************************/
  4.  
  5. /*                 WinMain() - Start here. Main function                     */
  6.  
  7. /*****************************************************************************/
  8. int PASCAL WinMain(hInstance, hPrevInst, lpCmdLine, nCmdShow)
  9. HANDLE hInstance;
  10. HANDLE hPrevInst;
  11. LPSTR lpCmdLine;
  12. int nCmdShow;
  13. {
  14. MSG   msg;
  15.  
  16. if (!hPrevInst)                         /* Has application been initialized? */
  17.  
  18. if (!MainInit(hInstance))
  19.    return(NULL);                            /* Exits if unable to initialize */
  20.  
  21. hInst = hInstance;
  22.  
  23. hMenu  = LoadMenu(hInst, "MainMenu");
  24.  
  25. xScreen = GetSystemMetrics(SM_CXSCREEN);                  /* Get Screen size */
  26. yScreen = GetSystemMetrics(SM_CYSCREEN);
  27.  
  28. hMainWnd = CreateWindow("Main",                               /* window class */
  29.             "Read PCX File" ,
  30.             WS_OVERLAPPED | WS_SYSMENU |     /* window style just system menu */
  31.             WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
  32.             WS_CLIPCHILDREN, 
  33.             0,                       /* x position start at upper left corner */
  34.             0,                       /* y position start at upper left corner */
  35.             xScreen,                                     /* width full screen */
  36.             yScreen,                                    /* height full screen */
  37.             NULL,                                            /* parent handle */
  38.             hMenu,                              /* menu at top of main window */
  39.             hInstance,                                            /* instance */
  40.             NULL);                                         /* additional info */
  41.  
  42. if (!hMainWnd)                                     /* was the window created? */
  43.    return (NULL);
  44.  
  45. ShowWindow(hMainWnd, nCmdShow);                           /* Shows the window */
  46. UpdateWindow(hMainWnd);                             /* Sends WM_PAINT message */
  47.  
  48. while
  49.    (GetMessage(&msg, NULL, 0, 0))
  50.        {
  51.        TranslateMessage(&msg);                /* Translates virtual key codes */
  52.        DispatchMessage(&msg);                 /* Dispatches message to window */
  53.        }
  54. return (msg.wParam);                /* Returns the value from PostQuitMessage */
  55.  
  56. }
  57. /*~***************************************************************************/
  58.  
  59. /*                      MainInit() - Set up window                            */
  60.  
  61. /******************************************************************************/
  62. BOOL MainInit(hInstance)                                  /* current instance */
  63. HANDLE hInstance;
  64. {
  65. WNDCLASS  WndClass;
  66.  
  67. WndClass.style            = CS_HREDRAW | CS_VREDRAW;
  68. WndClass.lpfnWndProc      = MainProc;
  69. WndClass.hInstance        = hInstance;
  70. WndClass.hIcon            = NULL;
  71. WndClass.hCursor          = LoadCursor(NULL, IDC_ARROW);
  72. WndClass.hbrBackground    = GetStockObject(WHITE_BRUSH);
  73. WndClass.lpszMenuName     = "Main";
  74. WndClass.lpszClassName    = "Main";
  75. WndClass.cbClsExtra       = 0;
  76. WndClass.cbWndExtra       = 0;
  77.  
  78. if (!RegisterClass (&WndClass))
  79.    return (FALSE);                /* Returns result of registering the window */
  80.  
  81. }
  82. /*~***************************************************************************/
  83.  
  84. /*                            MainProc() Function                             */
  85.  
  86. /*****************************************************************************/
  87. long FAR PASCAL MainProc(hMainWnd, message, wParam, lParam)
  88. HWND hMainWnd;
  89. unsigned message;
  90. WORD wParam;
  91. LONG lParam;
  92. {
  93. FARPROC    lpProc;
  94. HDC        hDC;
  95.  
  96. switch (message) 
  97.    {
  98.    case WM_CREATE:
  99.       hMenu = GetSystemMenu (hMainWnd, FALSE); /* Add Choices To System Menu. */
  100.       ChangeMenu (hMenu, NULL, "A&bout WinPcx", ID_ABOUT, MF_APPEND | MF_STRING);
  101.    break;
  102.  
  103.    case WM_DESTROY:                       /* message: window being destroyed */
  104.           PostQuitMessage(0);
  105.    break;
  106.  
  107.    case WM_CLOSE:
  108.        DestroyWindow (hMainWnd);
  109.  
  110.        DeleteDC (BMapDC);
  111.        DeleteObject (BMap);
  112.  
  113.        DeleteDC (NewBMapDC);
  114.        DeleteObject (NewBMap);
  115.  
  116.        DeleteDC (TmpDC);
  117.        DeleteObject (TmpMap);
  118.  
  119.    break;
  120.         
  121. /*----------------------------------------------------------------------------*/
  122.         
  123.    case WM_COMMAND:
  124.       switch (wParam)
  125.       {
  126.       case IDM_FILE:                                         /* Read PCX File */
  127.          lpProc = MakeProcInstance (DoPictFileBox, hInst);
  128.          DialogBox (hInst, "RCPictFileBox", hMainWnd, lpProc);
  129.          FreeProcInstance (lpProc);
  130.  
  131.          hDC = GetDC (hMainWnd);
  132.          LoadBitMap(hDC);
  133.          ReleaseDC (hMainWnd, hDC);
  134.       break;
  135.  
  136.       case IDM_SHOW:                              /* Display Bitmap To Screen */
  137.          hDC = GetDC (hMainWnd);
  138.          if (NewBMap)                          /* Display Size Altered Bitmap */
  139.             BitBlt (hDC, 0, 0, xScreen, yScreen, NewBMapDC, 0, 0, SRCCOPY);
  140.          else                 /* Display Bitmap As Is, Not Larger Than Screen */
  141.             BitBlt (hDC, 0, 0, xScreen, yScreen, BMapDC, 0, 0, SRCCOPY);
  142.  
  143.          ReleaseDC (hMainWnd, hDC);
  144.       break;
  145.  
  146.       case IDM_CLEAR:                                         /* Clear Screen */
  147.          InvalidateRect (hMainWnd, NULL, TRUE);
  148.       break;
  149.       }
  150.    break;
  151.  
  152. /*----------------------------------------------------------------------------*/
  153.  
  154.    case WM_SYSCOMMAND:                  /* message: command from system menu */
  155.       switch (wParam)
  156.          {
  157.          case ID_ABOUT:
  158.             lpProc = MakeProcInstance(About, hInst);
  159.             DialogBox(hInst, "RCAboutBox", hMainWnd, lpProc);
  160.             FreeProcInstance(lpProc);
  161.          break;
  162.          }
  163.  
  164. /*----------------------------------------------------------------------------*/
  165.  
  166.    default:                                   /* Passes it on if unprocessed */
  167.        return (DefWindowProc(hMainWnd, message, wParam, lParam));
  168.    }                                                      /* End Main Switch */
  169.  return (TRUE);
  170. }
  171.  
  172. /*~***************************************************************************/
  173.  
  174. /*                   LoadBitMap() - Load Bitmap PCX File                     */
  175.  
  176. /*****************************************************************************/
  177. int LoadBitMap (hDC)
  178. HDC hDC;
  179. {
  180. int size, bsize, i, pos, y, j, BitInt, k, z, q, status;
  181. unsigned char Bits[128], Bit, BitValue, Bytes[320], List[4002];
  182. FILE *fd;
  183. unsigned char ColorMap[48], Filler[58];
  184. int Manuf, Ver, Enc, BpPixel, Xmin, Ymin, Xmax, Ymax, HRes, VRes, Resv;
  185. int NPlanes, PInfo, Tmp;
  186.  
  187. fd = fopen (PictureFile, "rb");
  188. if (fd == NULL)
  189.    {
  190.    sprintf (out, "ERROR - Opening %s File.", PictureFile);
  191.    MsgEx(hMainWnd);
  192.    return (FALSE);
  193.    }
  194.  
  195. Manuf = fgetc(fd);                                      /* Get Manufacture ID */
  196. Ver = fgetc(fd);                                        /* Get Version Number */
  197. Enc = fgetc(fd);                                         /* Get Encoding Used */
  198. BpPixel = fgetc(fd);                                    /* Get Bits Per Pixel */
  199. fread (&Xmin, 2, 1, fd);                            /* Get Picture Dimensions */
  200. fread (&Ymin, 2, 1, fd);
  201. fread (&Xmax, 2, 1, fd);
  202. fread (&Ymax, 2, 1, fd);
  203. fread (&HRes, 2, 1, fd);                         /* Get Horizontal Resolution */
  204. fread (&VRes, 2, 1, fd);                           /* Get Vertical Resolution */
  205. fread (ColorMap, 1, 48, fd);                     /* Get Color Palette Setting */
  206. Resv = fgetc(fd);                                                 /* Reserved */
  207. NPlanes = fgetc(fd);                            /* Get Number Of Color Planes */
  208. fread (&BpLine, 2, 1, fd);                               /* Get Bits Per Line */
  209. fread (&PInfo, 2, 1, fd);                                 /* Get Palette Info */
  210. fread (Filler, 1, 58, fd);                                     /* Blank Stuff */
  211.  
  212. /*----------------------------------------------------------------------------*/
  213.  
  214. Xsize = Xmax - Xmin + 1;                              /* Cal. Size Of Picture */
  215. Ysize = Ymax - Ymin + 1;
  216. TotalBytes = NPlanes * BpLine;                    /* Cal. Total Bytes In Line */
  217.  
  218. if (BMap)
  219.    {                                              /* delete previous tmp map */
  220.    DeleteDC (BMapDC);
  221.    DeleteObject (BMap);
  222.    }
  223.  
  224. BMapDC = CreateCompatibleDC (hDC);    
  225. if (BMapDC == NULL) 
  226.    {
  227.    strcpy (out, "ERROR - Creating DC");
  228.    MsgEx (hMainWnd);
  229.    return (FALSE);
  230.    }
  231.  
  232. BMap = CreateCompatibleBitmap (hDC, Xsize, Ysize);
  233. if (BMap == NULL)
  234.    {
  235.    strcpy (out, "ERROR - Creating Bitmap");
  236.    MsgEx (hMainWnd);
  237.    return (FALSE);
  238.    }
  239.  
  240. if (BMap == NULL) 
  241.    {             /* NOT ENOUGH MEMORY!! */
  242.    strcpy (out, "ERROR - Not Enough Memory For Bitmap");
  243.    MsgEx (hMainWnd);
  244.    return(FALSE);
  245.    };
  246.  
  247. SelectObject(BMapDC,BMap);
  248.  
  249. if ((status = CreateTmpMap (hDC, Xsize, 1, 0xff)) == -1)
  250.    return (FALSE);
  251.  
  252. for ( y = 0; y < Ysize; y ++)                                 /* Clear Bitmap */
  253.    BitBlt (BMapDC, 0, y, Xsize, 1, TmpDC, 0, 0, SRCCOPY);
  254.  
  255. size = fread (List, 1, 4000, fd);   /* Read a 4000 byte chunk to be processed */
  256.  
  257. y = pos = 0;
  258. while ((size >= 0) && (y < Ysize))
  259.    {
  260.    i = 0;
  261.    while (i < TotalBytes)
  262.        {
  263.        BitInt = (int)List[pos];
  264.        pos ++; 
  265.        size --;
  266.        if (size <= 0) 
  267.            {
  268.            size = fread (List, 1, 4000, fd);
  269.            if (size <= 0) 
  270.                goto DONE;
  271.  
  272.            pos = 0;
  273.            }
  274.  
  275.        if ((BitInt & 192) == 192) 
  276.            {                                           /* Run length encoded */
  277.            bsize = BitInt & 63;                      /* Get # of repetitions */
  278.            for (j = i; j < bsize + i; j ++) Bytes[j] = List[pos];
  279.            pos ++;
  280.            size --;
  281.            i = i + bsize;
  282.            }
  283.  
  284.        else
  285.            {                                            /* Actual pixel data */
  286.            Bytes[i] = List[pos-1];
  287.            i ++;
  288.            }
  289.  
  290.        if (size <= 0) 
  291.            {
  292.            size = fread (List, 1, 4000, fd);
  293.            if (size <= 0) 
  294.                goto DONE;
  295.  
  296.            pos = 0;
  297.            }
  298.        }                               /* end while still reading 1 scan line */
  299.    y ++;
  300.  
  301.    for (z = 0; z < BpLine; z ++)                                 /* Color Fix */
  302.       {
  303.       Bit = Bytes[z];
  304.       Bytes[z] = Bytes[z + (BpLine * 2)];
  305.       Bytes[z + (BpLine * 2)] = Bit;
  306.       }
  307.  
  308. Tmp = TotalBytes - BpLine;                             /* Fix If Only 1 Plane */
  309. if (Tmp == 0)
  310.    Tmp = BpLine;
  311.  
  312.    SetBitmapBits (TmpMap, (LONG)Tmp, (LPSTR)Bytes);
  313.                            
  314.    if (y > 0)
  315.        BitBlt (BMapDC, 0, y, Xsize, 1, TmpDC, 0, 0, SRCCOPY);
  316.    }                                                   /* End for y=0 to Rows */
  317.  
  318. /*----------------------------------------------------------------------------*/
  319.  
  320. DONE:
  321.  
  322.    if (Xsize > xScreen || Ysize > yScreen)         /* Shrink Bitmap If Larger */
  323.       {                                                /* Than Screen Ex. CGA */
  324.       if (NewBMap)
  325.          {                                         /* delete previous tmp map */
  326.          DeleteDC (NewBMapDC);
  327.          DeleteObject (NewBMap);
  328.          }
  329.  
  330.       NewBMapDC = CreateCompatibleDC (hDC);    
  331.       if (NewBMapDC == NULL) 
  332.          {
  333.          strcpy (out, "ERROR - Creating New DC");
  334.          MsgEx (hMainWnd);
  335.          return (FALSE);
  336.          }
  337.  
  338.       NewBMap = CreateCompatibleBitmap (hDC, xScreen, yScreen);
  339.       if (NewBMap == NULL)
  340.          {
  341.          strcpy (out, "ERROR - Creating New Bitmap");
  342.          MsgEx (hMainWnd);
  343.          return (FALSE);
  344.          }
  345.  
  346.       if (NewBMap == NULL) 
  347.          {             /* NOT ENOUGH MEMORY!! */
  348.          strcpy (out, "ERROR - Not Enough Memory For New Bitmap");
  349.          MsgEx (hMainWnd);
  350.          return(FALSE);
  351.          };
  352.  
  353.       SelectObject (NewBMapDC, NewBMap);
  354.  
  355.       StretchBlt (NewBMapDC, 0, 0, xScreen, yScreen, 
  356.                   BMapDC, 0, 0, Xsize, Ysize, SRCCOPY);
  357.  
  358.       DeleteDC (BMapDC);                             /* Get Rid Of Out Bitmap */
  359.       DeleteObject (BMap);
  360.       }
  361.  
  362. fclose (fd);                                                    /* Close File */
  363. return (TRUE);
  364. }
  365. /******************************************************************************/
  366.  
  367. /*                CreateTmpMap() - Create Tmp. Bitmap                         */
  368.  
  369. /******************************************************************************/
  370. int CreateTmpMap (hDC, x, y, FILL)
  371. HDC hDC;
  372. int x,y;
  373. unsigned char FILL;
  374. {
  375. int j, Tmp;
  376. unsigned char Bytes[320];
  377.  
  378. if (TmpMap) 
  379.    {                                              /* delete previous tmp map */
  380.    DeleteDC (TmpDC);
  381.    DeleteObject (TmpMap);
  382.    }
  383.  
  384. TmpDC = CreateCompatibleDC (hDC);
  385. if (TmpDC == NULL) 
  386.    {
  387.    strcpy (out, "ERROR - Creating Tmp DC");
  388.    MsgEx (hMainWnd);
  389.    return(-1);
  390.    }
  391.  
  392. TmpMap = CreateDiscardableBitmap (hDC, x, y);
  393. if (TmpMap == NULL) 
  394.    {
  395.    strcpy (out, "ERROR - Creating Tmp Bitmap");
  396.    MsgEx (hMainWnd);
  397.    return(-1);
  398.    }
  399.  
  400. SelectObject (TmpDC, TmpMap);
  401. for (j = 0; j < 320; j ++) 
  402.    Bytes[j] = (unsigned char)FILL;
  403.  
  404. SetBitmapBits (TmpMap, (LONG)240, (LPSTR)Bytes);
  405. return(0);
  406. }
  407.  
  408. /*~***************************************************************************/
  409.  
  410. /*                   DoPictFileBox() - Get New PCX File                      */
  411.  
  412. /*****************************************************************************/
  413. BOOL FAR PASCAL DoPictFileBox(hWnd, message, wParam, lParam)
  414. HWND hWnd;
  415. unsigned message;
  416. WORD wParam;
  417. LONG lParam;
  418. {
  419. HDC            hDC;
  420. static char    FileBuf[200];
  421.  
  422. switch (message)
  423.    {
  424.    case WM_INITDIALOG:                      /* message: intialize dialog box */
  425.                                                   /* Load Files Into ListBox */
  426.        strcpy (FileBuf, "*.pcx");
  427.        DlgDirList (hWnd, FileBuf, LB_RESTORE, ID_RESTOREPATH, 0x4010);
  428.        SetFocus (GetDlgItem (hWnd, LB_RESTORE));
  429.  
  430.        return (FALSE);
  431.    break;
  432.  
  433. /*---------------------------------------------------------------------------*/
  434.  
  435.    case WM_COMMAND:                           /* message : recived a command */
  436.        switch(wParam)
  437.            {
  438.            case LB_RESTORE:
  439.                if (HIWORD(lParam) != 2)              /* Double click on item */
  440.            break;
  441.  
  442. /*---------------------------------------------------------------------------*/
  443.  
  444.            case IDOK: 
  445.                ok = DlgDirSelect (hWnd, FileBuf, LB_RESTORE);
  446.                if (ok != 0)                             /* Directory Selected */
  447.                    {
  448.                    strcat(FileBuf, "*.pcx");             /* Add *.pcx To Path */
  449.                                                     /* Put Files Into ListBox */
  450.                    DlgDirList(hWnd, FileBuf, LB_RESTORE,ID_RESTOREPATH,0x4010);
  451.                    break;
  452.                    }
  453.  
  454.                sprintf( out, "Load PCX File %s", FileBuf);
  455.                strcpy (buffer, "PCX File");
  456.                ok = MsgYN (hWnd);
  457.  
  458.                if (ok == IDNO)
  459.                    return(FALSE);                          /* Don't Load File */
  460.                   
  461.                EndDialog(hWnd, TRUE);
  462.  
  463.                strcpy (PictureFile, FileBuf);
  464.  
  465.                hDC = GetDC (hMainWnd);
  466.                LoadCursor (hInst, IDC_WAIT);
  467.                LoadBitMap (hDC);
  468.                LoadCursor (hInst, IDC_ARROW);
  469.  
  470.                ReleaseDC (hMainWnd, hDC);
  471.                return(TRUE);
  472.            break;
  473.  
  474. /*---------------------------------------------------------------------------*/
  475.  
  476.            case IDCANCEL:
  477.                EndDialog(hWnd, FALSE);
  478.                return(FALSE);
  479.            break;
  480.            }                                           /* End Command Switch */
  481.        default:
  482.            return(FALSE);
  483.            break;
  484.    }                                                      /* End Main Switch */
  485. }
  486. /*****************************************************************************/
  487.  
  488. /*            void MsgEx()    Message Box With OK And Exclamation            */
  489.  
  490. /*****************************************************************************/
  491. void MsgEx(hWnd)
  492. HWND   hWnd;
  493. {
  494. MessageBeep(0);
  495. MessageBox(hWnd, out, buffer, MB_OK | MB_ICONEXCLAMATION);
  496. }
  497.  
  498. /*****************************************************************************/
  499.  
  500. /*           int MsgYN()    Message Box With YES, NO And Question            */
  501.  
  502. /*****************************************************************************/
  503. int MsgYN(hWnd)
  504. HWND   hWnd;
  505. {
  506. ok = MessageBox(hMainWnd, out, buffer, MB_YESNO | MB_ICONQUESTION);
  507.  
  508. if (ok == IDYES)
  509.       return(IDYES);                              /* User Pressed YES Button */
  510.  
  511. else
  512.    return(IDNO);                                   /* User Pressed NO Button */
  513.  
  514. }
  515. /*~***************************************************************************/
  516.  
  517. /*                     About() Function - For About Box                      */
  518.  
  519. /*****************************************************************************/
  520. BOOL FAR PASCAL About(hAboutWnd, message, wParam, lParam)
  521. HWND hAboutWnd;
  522. unsigned message;
  523. WORD wParam;
  524. LONG lParam;
  525. {
  526.  switch (message)
  527.    {
  528.    case WM_INITDIALOG:                       /* Message: Intialize Dialog Box */
  529.        return (TRUE);
  530.  
  531.    case WM_COMMAND:                            /* Message : Recived A Command */
  532.  
  533.       if (wParam == IDOK)
  534.          {                                              /* "OK" Box Selected? */
  535.          EndDialog(hAboutWnd, NULL);                  /* Exits The Dialog Box */
  536.          return (TRUE);
  537.          }
  538.    break;
  539.  
  540.    }
  541. return (FALSE);
  542. }                                                 /* Didn't Process A Message */
  543.  
  544.  
  545.